package cn.bone.ou.data.service.impl;

import cn.bone.base.annotation.MyLogger;
import cn.bone.base.auth.JWTInfo;
import cn.bone.base.bean.CommonResult;
import cn.bone.base.bean.DataGrid;
import cn.bone.base.bean.Ref;
import cn.bone.base.utils.TimeUtil;
import cn.bone.ou.community.bean.CommunityMng;
import cn.bone.ou.community.mapper.CommunityMngMapper;
import cn.bone.ou.data.bean.CommunityNotice;
import cn.bone.ou.data.bean.CommunityNoticeDetail;
import cn.bone.ou.data.dto.NoticeDto;
import cn.bone.ou.data.mapper.CommunityNoticeDetailMapper;
import cn.bone.ou.data.mapper.CommunityNoticeMapper;
import cn.bone.ou.data.param.NoticeParam;
import cn.bone.ou.data.service.INoticeService;
import cn.bone.ou.data.vo.NoticeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NoticeServiceImpl implements INoticeService {

    @Resource
    private CommunityNoticeMapper noticeMapper;
    @Resource
    private CommunityNoticeDetailMapper noticeDetailMapper;
    @Resource
    private CommunityMngMapper mngMapper;

    /**获取列表*/
    public CommonResult<DataGrid<NoticeVo.ListVo>> list(NoticeParam.ListParam param){

        List<NoticeDto.ListDto> dtos = noticeMapper.list(param.getCommunityId(),
                param.getStart(), param.getNum(),
                param.getTitle(), param.getState());
        int count = noticeMapper.countList(param.getCommunityId(),
                param.getTitle(),param.getState());

        List<NoticeVo.ListVo> vos = new ArrayList<>();
        for(NoticeDto.ListDto dto : dtos){
            NoticeVo.ListVo vo = new NoticeVo.ListVo();
            vo.setNoticeId(dto.getNoticeId());
            vo.setTitle(dto.getTitle());
            vo.setState(dto.getState());
            vo.setPageViews(dto.getPageViews());
            vo.setPublishTime(dto.getPublishTime()==null?"": TimeUtil.formaterYYYYMMddHHmm(dto.getPublishTime()));
            vo.setPublishUserName(dto.getPublishUserName());
            vos.add(vo);
        }
        DataGrid<NoticeVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**添加公告*/
    @MyLogger(msg = "新增公告")
    public CommonResult<Void> add(NoticeParam.AddParam param){
        JWTInfo jwtInfo = JWTInfo.get();
        CommunityNotice notice = new CommunityNotice();
        notice.setCommunityId(param.getCommunityId());
        notice.setTitle(param.getTitle());
        notice.setState(0);
        notice.setPageViews(0);
        notice.setCreateUserId(jwtInfo.getUserId());
        notice.setCreateTime(new Date());
        noticeMapper.insert(notice);

        CommunityNoticeDetail detail = new CommunityNoticeDetail();
        detail.setNoticeId(notice.getId());
        detail.setContent(param.getContent());
        noticeDetailMapper.insert(detail);

        return Ref.ok();
    }

    /**修改公告*/
    @MyLogger(msg = "修改公告")
    public CommonResult<Void> modify(NoticeParam.ModifyParam param){
        CommunityNotice notice = noticeMapper.selectById(param.getNoticeId());
        if(notice==null){
            return Ref.error("错误的公告编号");
        }
        CommunityNoticeDetail detail = noticeDetailMapper.getByNoticeId(param.getNoticeId());
        if(detail==null){
            return Ref.error("错误的公告数据");
        }

        if(notice.getState()==1){
            return Ref.error("已发布的公告不能修改");
        }
        notice.setTitle(param.getTitle());
        noticeMapper.updateById(notice);

        detail.setContent(param.getContent());
        noticeDetailMapper.updateById(detail);
        return Ref.ok();
    }

    /**删除公告*/
    @MyLogger(msg = "删除公告")
    public CommonResult<Void> delete(NoticeParam.DeleteParam param){
        CommunityNotice notice = noticeMapper.selectById(param.getNoticeId());
        if(notice==null){
            return Ref.error("错误的公告编号");
        }
        CommunityNoticeDetail detail = noticeDetailMapper.getByNoticeId(param.getNoticeId());
        if(detail==null){
            return Ref.error("错误的公告数据");
        }

        if(notice.getState()==1){
            return Ref.error("已发布的公告不能删除");
        }
        noticeMapper.deleteById(param.getNoticeId());
        noticeDetailMapper.deleteById(detail.getId());
        return Ref.ok();
    }

    /**发布公告*/
    @MyLogger(msg = "发布公告")
    public CommonResult<Void> publish(NoticeParam.PublishParam param){
        CommunityNotice notice = noticeMapper.selectById(param.getNoticeId());
        if(notice==null){
            return Ref.error("错误的公告编号");
        }
        if(notice.getState()==1){
            return Ref.error("该公告已经发布过了");
        }
        JWTInfo jwtInfo = JWTInfo.get();
        notice.setState(1);
        notice.setPublishTime(new Date());
        notice.setPublishUserId(jwtInfo.getUserId());
        noticeMapper.updateById(notice);
        return Ref.ok();
    }

    /**取消发布*/
    @MyLogger(msg = "取消发布公告")
    public CommonResult<Void> unpublish(NoticeParam.UnpublishParam param){
        CommunityNotice notice = noticeMapper.selectById(param.getNoticeId());
        if(notice==null){
            return Ref.error("错误的公告编号");
        }
        if(notice.getState()==0){
            return Ref.error("该公告还未发布");
        }
        notice.setState(0);
        notice.setPublishUserId(null);
        notice.setPublishTime(null);
        noticeMapper.updateById(notice);

        return Ref.ok();
    }

    /**获取公告详情*/
    public CommonResult<NoticeVo.DetailVo> detail(NoticeParam.DetailParam param){
        CommunityNotice notice = noticeMapper.selectById(param.getNoticeId());
        if(notice==null){
            return Ref.error("错误的公告编号");
        }
        CommunityNoticeDetail detail = noticeDetailMapper.getByNoticeId(param.getNoticeId());
        if(detail==null){
            return Ref.error("错误的公告数据");
        }
        NoticeVo.DetailVo vo = new NoticeVo.DetailVo();
        vo.setNoticeId(notice.getId());
        vo.setTitle(notice.getTitle());
        vo.setContent(detail.getContent());
        vo.setPageViews(notice.getPageViews());
        if(notice.getPublishTime()==null){
            vo.setPublishTime(TimeUtil.formaterYYYYMMdd(new Date()));
            CommunityMng mng = mngMapper.selectById(notice.getCreateUserId());
            if(mng!=null){
                vo.setPublishUserName(mng.getRealName());
            }
        }else{
            vo.setPublishTime(TimeUtil.formaterYYYYMMdd(notice.getPublishTime()));
            CommunityMng mng = mngMapper.selectById(notice.getPublishUserId());
            if(mng!=null){
                vo.setPublishUserName(mng.getRealName());
            }
        }
        return Ref.ok(vo);
    }
}
